<template>
  <div class="admin-profile">
    <div class="profile-header">
      <h2 class="title">基本资料</h2>
      <el-button type="primary" @click="handleEdit">编辑资料</el-button>
    </div>
    
    <div class="profile-content">
      <div class="info-section">
        <div class="info-item">
          <span class="label">用户名</span>
          <span class="value">{{ profileData.username }}</span>
        </div>
        <div class="info-item">
          <span class="label">邮箱</span>
          <span class="value">{{ profileData.email }}</span>
        </div>
      </div>
    </div>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="editDialogVisible"
      title="编辑基本资料"
      width="500px"
      @close="resetEditForm"
    >
      <el-form
        ref="editFormRef"
        :model="editForm"
        :rules="editRules"
        label-width="80px"
        class="edit-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getAdminProfile, updateAdminProfile } from '@/api/admin'

// 个人资料数据
const profileData = ref({
  adminId: '',
  username: '',
  email: '',
  creationDate: '',
  lastLogin: '',
  status: 0
})

// 编辑对话框相关
const editDialogVisible = ref(false)
const editFormRef = ref(null)
const editForm = ref({
  username: '',
  email: ''
})

// 表单验证规则
const editRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

// 获取个人资料
const fetchProfile = async () => {
  try {
    const res = await getAdminProfile()
    if (res.code === 0) {
      profileData.value = res.data
    } else {
      ElMessage.error(res.msg || '获取个人资料失败')
    }
  } catch (error) {
    console.error('获取个人资料失败:', error)
    ElMessage.error('获取个人资料失败')
  }
}

// 打开编辑对话框
const handleEdit = () => {
  editForm.value = {
    username: profileData.value.username,
    email: profileData.value.email
  }
  editDialogVisible.value = true
}

// 重置编辑表单
const resetEditForm = () => {
  editForm.value = {
    username: '',
    email: ''
  }
  if (editFormRef.value) {
    editFormRef.value.resetFields()
  }
}

// 保存个人资料
const handleSave = async () => {
  if (!editFormRef.value) return
  
  await editFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const params = {
          adminId: profileData.value.adminId,
          username: editForm.value.username,
          email: editForm.value.email
        }
        
        const res = await updateAdminProfile(params)
        if (res.code === 0) {
          ElMessage.success('保存成功')
          editDialogVisible.value = false
          fetchProfile() // 刷新个人资料
        } else {
          ElMessage.error(res.msg || '保存失败')
        }
      } catch (error) {
        console.error('保存失败:', error)
        ElMessage.error('保存失败')
      }
    }
  })
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchProfile()
})
</script>

<style scoped>
.admin-profile {
  padding: 32px;
  background-color: #fff;
  min-height: calc(100vh - 64px);
}

.profile-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
}

.title {
  font-size: 22px;
  font-weight: 500;
  color: #303133;
  margin: 0;
}

.profile-content {
  max-width: 800px;
}

.info-section {
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 32px;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.label {
  width: 120px;
  color: #606266;
  font-size: 15px;
  font-weight: 500;
}

.value {
  flex: 1;
  color: #303133;
  font-size: 15px;
}

.edit-form {
  padding: 20px 40px;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
}

:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}

:deep(.el-input) {
  width: 100%;
}

:deep(.el-dialog__body) {
  padding: 0;
}

:deep(.el-dialog__header) {
  margin: 0;
  padding: 20px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-dialog__title) {
  font-size: 18px;
  font-weight: 500;
}

:deep(.el-button) {
  padding: 9px 20px;
}
</style> 